package vip.kqsy.cloud.blog.server.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.kqsy.cloud.blog.server.pojo.dto.CarouselAddDTO;
import vip.kqsy.cloud.blog.server.pojo.dto.PageCarouselDTO;
import vip.kqsy.cloud.blog.server.pojo.entity.Carousel;
import vip.kqsy.cloud.blog.server.pojo.vo.PageCarouselVO;
import vip.kqsy.cloud.blog.server.service.CarouselService;
import vip.kqsy.cloud.core.web.res.R;
import vip.kqsy.cloud.core.web.res.ResPage;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cenjunjie
 * @since 2023-06-23
 */
@RestController
@RequestMapping("/carousel")
@Api(tags = "轮播图")
@Valid
@RequiredArgsConstructor
public class CarouselController {

    private final CarouselService carouselService;

    @PostMapping("add")
    @ApiOperation("添加轮播图")
    public R<Void> add(@Valid CarouselAddDTO dto) {
        carouselService.add(dto);
        return R.ok();
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    public R<Void> delete(@Parameter(description = "博客ID") @NotNull Long id) {
        carouselService.removeById(id);
        return R.ok();
    }

    @GetMapping("page")
    @ApiOperation("分页查询轮播图")
    public R<ResPage<PageCarouselVO>> page(PageCarouselDTO dto) {
        return R.ok(carouselService.page(dto));
    }

    @GetMapping("all")
    @ApiOperation("查询轮播图")
    public R<List<PageCarouselVO>> all() {
        List<Carousel> list = carouselService.list();
        return R.ok(BeanUtil.copyToList(list, PageCarouselVO.class));
    }
}
